package org.jeecg.modules.my.cblCgd2.service.impl;

import org.jeecg.modules.my.cblCgd2.entity.CblCgdZb;
import org.jeecg.modules.my.cblCgd2.entity.CblCgd;
import org.jeecg.modules.my.cblCgd2.mapper.CblCgdMapper;
import org.jeecg.modules.my.cblCgd2.mapper.CblCgdZbMapper;
import org.jeecg.modules.my.cblCgd2.service.ICblCgdZbService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 采购单主表 
 * @Author: jeecg-boot
 * @Date:   2020-10-04
 * @Version: V1.0
 */
@Service
public class CblCgdZbServiceImpl extends ServiceImpl<CblCgdZbMapper, CblCgdZb> implements ICblCgdZbService {

	@Autowired
	private CblCgdZbMapper cblCgdZbMapper;
	@Autowired
	private CblCgdMapper cblCgdMapper;
	
	@Override
	@Transactional
	public void saveMain(CblCgdZb cblCgdZb, List<CblCgd> cblCgdList) {
		cblCgdZbMapper.insert(cblCgdZb);
		if(cblCgdList!=null && cblCgdList.size()>0) {
			for(CblCgd entity:cblCgdList) {
				//外键设置
				entity.setCgdZbId(cblCgdZb.getId());
				cblCgdMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(CblCgdZb cblCgdZb,List<CblCgd> cblCgdList) {
		cblCgdZbMapper.updateById(cblCgdZb);
		
		//1.先删除子表数据
		cblCgdMapper.deleteByMainId(cblCgdZb.getId());
		
		//2.子表数据重新插入
		if(cblCgdList!=null && cblCgdList.size()>0) {
			for(CblCgd entity:cblCgdList) {
				//外键设置
				entity.setCgdZbId(cblCgdZb.getId());
				cblCgdMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		cblCgdMapper.deleteByMainId(id);
		cblCgdZbMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			cblCgdMapper.deleteByMainId(id.toString());
			cblCgdZbMapper.deleteById(id);
		}
	}
	
}
